package zzz.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ser.std.MapProperty;

import zzz.mapper.PostMapper;
import zzz.mapper.ReplyMapper;
import zzz.mapper.UserMapper;
import zzz.model.Post;
import zzz.model.User;
import zzz.service.PostService;

@Service
@Transactional  
public class PostServiceImpl implements PostService{

	@Autowired
	private PostMapper mapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ReplyMapper replyMapper;

	@Override
	public List<Post> findAllPost() {
		// TODO Auto-generated method stub
		List<Post> allPost=mapper.findAllPost();
		for (int i = 0; i < allPost.size(); i++) {
			Post p = allPost.get(i);
			SimpleDateFormat sf = new SimpleDateFormat(" yyyy-MM-dd");
			String str = sf.format(p.getPostTime());
			User user = userMapper.getUserById(String.valueOf(p.getUserId()));
			int count=replyMapper.countReplyByPost(String.valueOf(p.getPostId()));
			p.setPostCode(str);
			p.setUser(user);
			p.setReplyCnt(count);
		}
		return allPost;
	}

	@Override
	public Post findPostById(String postId) {
		// TODO Auto-generated method stub
		Post post=mapper.findPostById(postId);
		SimpleDateFormat sf=new SimpleDateFormat(" yyyy-MM-dd");
		String str=sf.format(post.getPostTime());
		User user=userMapper.getUserById(String.valueOf(post.getUserId()));
		int count=replyMapper.countReplyByPost(String.valueOf(post.getPostId()));
		post.setPostCode(str);
		post.setUser(user);
		post.setReplyCnt(count);
		return post;
	}

	@Override
	public void newPost(Post post) {
		// TODO Auto-generated method stub
		mapper.addPost(post);
	}

	@Override
	public List<Post> findPostByUserId(String userId) {
		// TODO Auto-generated method stub
		List<Post> postsByUser=mapper.findPostByUserId(userId);
		for (int i = 0; i < postsByUser.size(); i++) {
			Post p = postsByUser.get(i);
			SimpleDateFormat sf = new SimpleDateFormat(" yyyy-MM-dd");
			String str = sf.format(p.getPostTime());
			User user = userMapper.getUserById(String.valueOf(p.getUserId()));
			int count=replyMapper.countReplyByPost(String.valueOf(p.getPostId()));
			p.setPostCode(str);
			p.setUser(user);
			p.setReplyCnt(count);
		}
		return postsByUser;
	}

	@Override
	public void deletePost(String id) {
		// TODO Auto-generated method stub
		mapper.manageDelPost(id);
	}

	@Override
	public int updatePost(Post post) {
		// TODO Auto-generated method stub
		int i=mapper.updatePost(post);
		return i;
	}

	@Override
	public int addPost(Post post) {
		// TODO Auto-generated method stub
		int i=mapper.manageAddPost(post);
		return i;
	}

	@Override
	public List<Post> findNewPost() {
		// TODO Auto-generated method stub
		List<Post> newPosts=mapper.findNewPost();
		for(int i=0;i<newPosts.size();i++){
			Post p = newPosts.get(i);
			SimpleDateFormat sf=new SimpleDateFormat(" yyyy-MM-dd");
			String str=sf.format(newPosts.get(i).getPostTime());
			int count=replyMapper.countReplyByPost(String.valueOf(p.getPostId()));
			p.setPostCode(str);
			p.setReplyCnt(count);
			}
		return newPosts;
	}
}
